package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.SupportsDao;
import com.ngm.tea.tea.entity.Supports;
import com.ngm.tea.tea.service.SupportsService;

/**
 * 服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("supportsService")
public class SupportsServiceImpl implements SupportsService {
	
	@Resource
	private SupportsDao supportsDao;

	public SupportsDao getSupportsDao() {
		return supportsDao;
	}

	@Override
	public void insert(Supports supports) {
		supportsDao.insert(supports);
	}

	@Override
	public void deleteById(Long id) {
		supportsDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			supportsDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Supports supports) {
		supportsDao.delete(supports);
	}

	@Override
	public void update(Supports supports) {
		supportsDao.update(supports);
	}

	@Override
	public Supports selectOne(Long id) {
		return supportsDao.selectOne(id);
	}

	@Override
	public List<Supports> selectAll() {
		return supportsDao.selectAll();
	}

	@Override
	public List<Supports> selectList(Supports supports, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(supports));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return supportsDao.selectList(map);
	}

	@Override
	public Long count(Supports supports) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(supports));
		return supportsDao.count(map);
	}
	
	public String makeStr(Supports supports) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(supports.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + supports.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + supports.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(supports.getDescription())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("description LIKE '%" + supports.getDescription()+"%'");
			} else {
				sb.append(" AND description LIKE '%" + supports.getDescription()+"%'");
			}
		}
		if(!StringUtils.isEmpty(supports.getType())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("type LIKE '%" + supports.getType()+"%'");
			} else {
				sb.append(" AND type LIKE '%" + supports.getType()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public List<Supports> selectAllByGoodsId(String ids) {
		if(!StringUtils.isEmpty(ids)) {
			List<Supports> list = new ArrayList<>();
			String[] _ids = ids.split(",");
			for(String id : _ids) {
				Supports supports = supportsDao.selectOne(new Long(id));
				list.add(supports);
			}
			return list;
		} else {
			return null;
		}
	}
	
}